import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author xiexiaojin
 * @date 2025-07-25 14:37
 */
public class Base64Utils {
    /**
     * 将图片转为Base64编码
     * @param imgpath
     * @return
     */
    public static String imageToBase64Str(String imgpath) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgpath);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
            // 将图片转为base64编码
            BASE64Encoder encoder = new BASE64Encoder();

            return encoder.encode(data);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("将图片转换为Base64编码异常");
            return "异常";
        }
    }
}
